package com.ocl.www.controller;

import com.ocl.www.basic.BasicController;
import com.ocl.www.domain.Lyarticle;
import com.ocl.www.domain.PageObject;
import com.ocl.www.service.LyArticleService;
import net.sf.json.JSONArray;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/29.
 */
@RestController
@RequestMapping("/lyarticle")
public class LyarticleController  extends BasicController {

    @Autowired
    private EhCacheManager em;

    private Logger logger = Logger.getLogger(LyarticleController.class);
    @Autowired
    private LyArticleService lyArticleService;

    @RequestMapping(value="/getArticle", method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Map<String,Object> getArticle(@RequestBody Map<String,Object> map){

        Lyarticle lyarticle = new Lyarticle();
        lyarticle.setId(Integer.parseInt(map.get("id").toString()));
        try {
            lyarticle = lyArticleService.getArticle(lyarticle);
            map.put("info",lyarticle);
            map.put("status","s");
        } catch (Exception e) {
            map.put("info",e.getMessage());
            map.put("status","e");
        }
        return map;

    }

    @RequestMapping(value="/selectArticleMoreInfo", method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Map<String,String> selectArticleMoreInfo(@RequestBody Map<String,String> map , HttpServletRequest request){


        try {
            logger.info("当前空闲内存1："+Runtime.getRuntime().freeMemory());
            logger.info("当前total内存："+Runtime.getRuntime().totalMemory());
            PageObject pageObject = new PageObject();
            pageObject.setStartLine(Integer.parseInt(map.get("startLine")));
            pageObject.setCount(Integer.parseInt(map.get("count")));

            List<Lyarticle> lyarticleList = lyArticleService.selectArticleMoreInfo(pageObject);

            if (lyarticleList.isEmpty()){
                map.put("status","s1");
                map.put("info", "没有更多了撒！");
            }
            String path = request.getServletContext().getRealPath("/");
            String picPath =  path + "WEB-INF"+ File.separator+"classes"+File.separator+"public";
            String suffixName = "";
            String filestr = "";
            File f = null;
            for (int i=0;i<lyarticleList.size();i++){

                logger.info(picPath);
                // 获取文件的后缀名
                suffixName = lyarticleList.get(i).getPicPath().substring(lyarticleList.get(i).getPicPath().lastIndexOf(".")+1);
                filestr =  picPath+lyarticleList.get(i).getPicPath();
                f = new File(filestr);
                logger.info("f.length():"+f.length());
                if (f.exists()){
                    lyarticleList.get(i).setPicPath("data:image/"+suffixName+";base64,"+FileController.GetImageStr(filestr));

                }else{
                    lyarticleList.get(i).setPicPath("data:image/png;base64,"+FileController.GetImageStr(path + "WEB-INF"+ File.separator+"classes"+File.separator+"static\\img\\xiufu.png"));

                }
                f = null;
            }
            //set JAVA_OPTS=-Xms500M -Xmx910M
            Long l = 0L;
            for (Lyarticle value : lyarticleList) {
                byte[] bytes = null;
                try {
                    // object to bytearray
                    ByteArrayOutputStream bo = new ByteArrayOutputStream();
                    ObjectOutputStream oo = new ObjectOutputStream(bo);
                    oo.writeObject(value);

                    bytes = bo.toByteArray();

                    bo.close();
                    oo.close();
                } catch (Exception e) {
                    System.out.println("translation" + e.getMessage());
                    e.printStackTrace();
                }

                l= l+bytes.length;
            }

            logger.info("最大内存："+Runtime.getRuntime().maxMemory());
            logger.info("当前空闲内存2："+Runtime.getRuntime().freeMemory());
            logger.info("当前total内存2："+Runtime.getRuntime().totalMemory());
            logger.info("list大小："+l);
            map.put("status","s");
            map.put("info", String.valueOf(JSONArray.fromObject(lyarticleList)));
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","e");
            map.put("info",e.getMessage());
        }

        return map;

    }




    @RequestMapping(value="/selectArticleMoreInfo_old", method= RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Map<String,String> selectArticleMoreInfo_old(@RequestBody Map<String,String> map , HttpServletRequest request){


        try {
            logger.info("当前空闲内存1："+Runtime.getRuntime().freeMemory());
            logger.info("当前total内存："+Runtime.getRuntime().totalMemory());
            PageObject pageObject = new PageObject();
            pageObject.setStartLine(Integer.parseInt(map.get("startLine")));
            pageObject.setCount(Integer.parseInt(map.get("count")));
            pageObject.setArtId(Integer.parseInt(map.get("artId")));

            List<Lyarticle> lyarticleList = lyArticleService.selectArticleMoreInfo_old(pageObject);

            if (lyarticleList.isEmpty()){
                map.put("status","s1");
                map.put("info", "没有更多了撒！");
            }
            String path = request.getServletContext().getRealPath("/");
            String picPath =  path + "WEB-INF"+ File.separator+"classes"+File.separator+"public";
            String suffixName = "";
            String filestr = "";
            File f = null;
            for (int i=0;i<lyarticleList.size();i++){

                logger.info(picPath);
                // 获取文件的后缀名
                suffixName = lyarticleList.get(i).getPicPath().substring(lyarticleList.get(i).getPicPath().lastIndexOf(".")+1);
                filestr =  picPath+lyarticleList.get(i).getPicPath();
                f = new File(filestr);
                logger.info("f.length():"+f.length());
                if (f.exists()){
                    lyarticleList.get(i).setPicPath("data:image/"+suffixName+";base64,"+FileController.GetImageStr(filestr));

                }else{
                    lyarticleList.get(i).setPicPath("data:image/png;base64,"+FileController.GetImageStr(path + "WEB-INF"+ File.separator+"classes"+File.separator+"static\\img\\xiufu.png"));

                }
                f = null;
            }
            //set JAVA_OPTS=-Xms500M -Xmx910M
            Long l = 0L;
            for (Lyarticle value : lyarticleList) {
                byte[] bytes = null;
                try {
                    // object to bytearray
                    ByteArrayOutputStream bo = new ByteArrayOutputStream();
                    ObjectOutputStream oo = new ObjectOutputStream(bo);
                    oo.writeObject(value);

                    bytes = bo.toByteArray();

                    bo.close();
                    oo.close();
                } catch (Exception e) {
                    System.out.println("translation" + e.getMessage());
                    e.printStackTrace();
                }

                l= l+bytes.length;
            }

            logger.info("最大内存："+Runtime.getRuntime().maxMemory());
            logger.info("当前空闲内存2："+Runtime.getRuntime().freeMemory());
            logger.info("当前total内存2："+Runtime.getRuntime().totalMemory());
            logger.info("list大小："+l);
            map.put("status","s");
            map.put("info", String.valueOf(JSONArray.fromObject(lyarticleList)));
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","e");
            map.put("info",e.getMessage());
        }

        return map;

    }


}
